using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using System.IO;

namespace LanguageConfigurationSolution
{
    class LanguageConfigurationRootLandingPageFeatureReceiver : SPFeatureReceiver
    {
        private const string ROOTLANDINGPAGEFILE = "RootLandingPage.aspx";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;

            string path = Path.Combine(properties.Definition.RootDirectory, ROOTLANDINGPAGEFILE);
            FileStream filestream = new FileStream(path, FileMode.Open, FileAccess.Read);
            try
            {
                SPFile file = site.RootWeb.RootFolder.Files.Add(ROOTLANDINGPAGEFILE, filestream, true);


                SPFolder folder = site.RootWeb.RootFolder;
                folder.WelcomePage = file.Url;
                folder.Update();
            }
            finally
            {
                filestream.Close();
            }

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPSite site = properties.Feature.Parent as SPSite;
            if (site == null) return;


            SPFile file = null;
            try
            {
                file = site.RootWeb.RootFolder.Files[ROOTLANDINGPAGEFILE];
                if (file != null)
                {

                    SPFolder folder = site.RootWeb.RootFolder;
                    folder.WelcomePage = null;
                    folder.Update();


                    file.Delete();
                }
            }
            catch
            {

            }

        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {

        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {

        }
    }
}
